<?php

class WidgetLayoutController extends BackOfficeController
{
	/**
	 * @var string the default layout for the views. Defaults to '//layouts/column2', meaning
	 * using two-column layout. See 'protected/views/layouts/column2.php'.
	 */
	public $layout='//layouts/column2';

	/**
	 * Displays a particular model.
	 * @param integer $id the ID of the model to be displayed
	 */
	public function actionView($id)
	{
		$this->render('view',array(
			'model'=>FSM::run('Cms.WidgetLayout.get', array('id'=>$id))->model,
		));
	}

	/**
	 * Creates a new model.
	 * If creation is successful, the browser will be redirected to the 'view' page.
	 */
	public function actionCreate()
	{
        $this->actionUpdate();
	}

	/**
	 * Updates a particular model.
	 * If update is successful, the browser will be redirected to the 'view' page.
	 * @param integer $id the ID of the model to be updated
	 */
	public function actionUpdate()
	{
        if (Yii::app()->request->IsPostRequest) {
            // save posted data
            $_POST['validateOnly'] = ($this->post('ajax','') == 'widget-layout-form');
            $result = FSM::run('Cms.WidgetLayout.save', $_POST);
            $model = $result->model; 

            if ($this->post('ajax','') == 'widget-layout-form'){
                echo $result->getActiveErrorMessages($result->model);
                Yii::app()->end();
            }   
            if (! $result->hasErrors())
            {
                $this->message = Yii::t('Core','Item has been saved successfully.');
                $this->redirect($this->createUrl('update',array('id' => $model->id)));
            }
        } else {
            // show edit form
            $id = $this->get('id', 0);
            if ($id == 0) {
                $model = new WidgetLayout();
                if (isset($_GET['widget_id']))
                    $model->widget_id = (int) $_GET['widget_id'];
            } else {
                $model = FSM::run('Cms.WidgetLayout.get', array('id' => $id))->model;
            }
        }
            
        $this->render('update', array('model' => $model));
	}

	/**
	 * Deletes a particular model.
	 * If deletion is successful, the browser will be redirected to the 'index' page.
	 * @param integer $id the ID of the model to be deleted
	 */
	public function actionDelete()
	{
        // we only allow deletion via POST request
        if (($id = $this->get('id',null)) !== null) {
            $ids = is_numeric($id) ? array($id) : explode(',',$id);
            
            // delete one or multiple objects given the list of object IDs
            $result = FSM::run('Cms.WidgetLayout.delete', array('ids' => $ids));
            if ($result->hasErrors()) {
                echo $result->getError('ErrorCode');
            } elseif(!Yii::app()->request->isAjaxRequest) {
                // only redirect user to the admin page if it is not an AJAX request
                $return = Yii::app()->request->getParam('returnUrl', false);
                $this->redirect($return ? urldecode($return) : array('admin'));
            }
        } else {
            throw new CHttpException(400,Yii::t('Core','Cannot delete item with the given ID.'));
        }
	}

	/**
	 * Lists all models.
	 */
	public function actionIndex()
	{
        $this->actionAdmin();
	}

	/**
	 * Manages all models.
	 */
	public function actionAdmin()
	{
        $model=new WidgetLayout('search');
        $model->unsetAttributes();  // clear any default values
        if(isset($_GET['WidgetLayout']))
            $model->attributes=$_GET['WidgetLayout'];

        $this->render('admin',array(
            'model'=>$model,
        ));
	}
    
    public function actionUnused()
    {
        $model=new WidgetLayout('unused');
        $model->unsetAttributes();
        
        if(isset($_GET['WidgetLayout']))
            $model->attributes=$_GET['WidgetLayout'];
            
        $this->render('unused', array('model'=>$model));
    }
}
